package com.liy.framework.web.rs.exception;

import com.liy.framework.common.extvo.ResultVO;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
@Provider
public class JaxrsExceptionMapper implements ExceptionMapper<Throwable> {
    protected static Logger log = LoggerFactory.getLogger(JaxrsExceptionMapper.class);

    public JaxrsExceptionMapper() {}

    public Response toResponse(Throwable exception) {
        log.error(exception.getMessage(), exception);
        com.liy.framework.web.rs.exception.ExceptionType expType = ExceptionManager.get(exception);
        ResultVO resultVO = new ResultVO(false, expType.getErrMessage(), expType.getErrCode());
        resultVO.put("timestamp", System.currentTimeMillis());
        return Response.status(Status.NOT_FOUND).entity(resultVO).encoding("UTF-8").build();
    }
}
